﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace Sustainalytics.Reporting.Framework
{
    public static class HTMLConvertor
    {
        public static string ConvertComment(string htmlComment)
        {
            if (string.IsNullOrWhiteSpace(htmlComment))
                return string.Empty;

            var sb = new StringBuilder();
            var doc = new HtmlDocument();


            //remove double line breaks from Canvas
            htmlComment = htmlComment.Replace("<br/></p>", "</p>").Replace("<br /></p>", "</p>").Replace("<br></p>", "</p>");

            doc.LoadHtml(htmlComment);

            foreach (var childNode in doc.DocumentNode.ChildNodes)
            {
                if (childNode.Name == "p")
                {
                    sb.AppendFormat("<p>{0}</p>", childNode.InnerHtml);
                }

                if (childNode.Name == "#text")
                {
                    sb.AppendFormat("<p>{0}</p>", childNode.InnerText.Replace("<", "&lt;").Replace(">", "&gt;"));
                }

                if (childNode.Name == "ul")
                {
                    HtmlUnorderedList(childNode, sb);
                }

                if (childNode.Name == "ol")
                {
                    HtmlOrderedList(childNode, sb);
                }

                if (childNode.Name == "div")
                {
                    if (!childNode.HasChildNodes) continue;

                    sb.Append("<p>");

                    foreach (var node in childNode.ChildNodes)
                    {
                        if (node.Name == "ul")
                        {
                            HtmlUnorderedList(childNode, sb);
                            continue;
                        }

                        if (node.Name == "ol")
                        {
                            HtmlOrderedList(childNode, sb);
                            continue;
                        }

                        if (node.Name == "#text")
                        {
                            sb.Append(node.InnerText);
                            continue;
                        }

                        sb.AppendFormat("<{0}>{1}</{2}>", node.Name, node.InnerHtml, node.Name);
                    }

                    sb.Append("</p>");
                }
            }

            var result = sb.ToString();

            return result;
        }

        private static void HtmlUnorderedList(HtmlNode node, StringBuilder sb)
        {
            if (!node.HasChildNodes)
                return;

            foreach (var childNode in node.ChildNodes.Where(n => n.Name == "li"))
            {
                sb.AppendFormat("<p>&nbsp;&nbsp;&nbsp;&nbsp;• <{0}>{1}</{2}></p>", childNode.Name, RemoveNewLineTagsFromLists(childNode).InnerHtml, childNode.Name);
            }
        }

        private static void HtmlOrderedList(HtmlNode node, StringBuilder sb)
        {
            if (!node.HasChildNodes)
                return;

            var number = 1;
            foreach (var childNode in node.ChildNodes.Where(cNode => cNode.Name == "li"))
            {
                sb.AppendFormat("<p>&nbsp;&nbsp;&nbsp;&nbsp;{0}. <{1}>{2}</{3}></p>", number, childNode.Name, RemoveNewLineTagsFromLists(childNode).InnerHtml, childNode.Name);
                number++;
            }
        }

        private static string[] newLineTags = new string[] { "<br>", "<br/>", "<p>", "</p>", "<div>", "</div>" };

        private static HtmlNode RemoveNewLineTagsFromLists(HtmlNode node)
        {
            foreach (var tag in newLineTags)
            {
                node.InnerHtml = node.InnerHtml.Replace(tag, "");
            }
            return node;
        }
    }
}
